package net.xo68.destiny.core.timingwheel.process;

import lombok.extern.slf4j.Slf4j;
import net.xo68.destiny.core.timingwheel.Timeout;

import java.time.LocalDateTime;
import java.util.HashMap;

/**
 * 处理器上下文
 * @author wuxie
 * @version 2020-10-23
 */
@Slf4j
public class EventProcessorContext<TP> extends HashMap<String,Object> {

    /**
     * 结果
     */
    public static final String KEY_RESULT="result";

    /**
     * 异常
     */
    public static final String KEY_EXCEPTION="exception";

    /**
     * 是否上报回复报文
     */
    public static final String KEY_RESULT_IS_REPLY="isReply";

    /**
     * 开始时间
     */
    public static final String KEY_START_TIME="startTime";

    /**
     * 结束时间
     */
    public static final String KEY_FINISH_TIME="finishTime";

    private final Timeout<TP> timeout;

    public EventProcessorContext(Timeout<TP> timeout) {
        this.timeout=timeout;
    }

    public Timeout<TP> timeout() {
        return timeout;
    }

    public TP triggerParameter(){
        return this.timeout.triggerParameter();
    }

    public void setResult(Object object){
        this.put(KEY_RESULT,object);
    }

    public <T> T getResult(){
        return (T) this.get(KEY_RESULT);
    }

    public void setException(Object object){
        this.put(KEY_EXCEPTION,object);
    }
    public <T> T getException(){
        return (T) this.get(KEY_EXCEPTION);
    }

    public void setReply(boolean reply){
        this.put(KEY_RESULT_IS_REPLY,reply);
    }
    public boolean isReply(){
        if(!this.containsKey(KEY_RESULT_IS_REPLY)){
            return true;
        }
        return (boolean) this.get(KEY_RESULT_IS_REPLY);
    }

    public void setStartTime(LocalDateTime startTime){
        this.put(KEY_START_TIME,startTime);
    }
    public LocalDateTime getStartTime(){
        return (LocalDateTime) this.get(KEY_START_TIME);
    }

    public void setFinishTime(LocalDateTime finishTime){
        this.put(KEY_FINISH_TIME,finishTime);
    }
    public LocalDateTime getFinishTime(){
        return (LocalDateTime) this.get(KEY_FINISH_TIME);
    }

}
